package com.yricky.indesk.ui.compose

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import com.yricky.indesk.model.pojo.AppEntrance
import com.yricky.indesk.model.pojo.emptyAppEntrance
import com.yricky.indesk.ui.compose.shapes.RoundRect
import com.yricky.indesk.utils.*

/**
 * @author Yricky
 * @date 2021/12/26
 */
@Preview(backgroundColor = 0xffffffff)
@Composable
fun MainGridAppList(
    modifier: Modifier = Modifier,
    row:Int = 5,
    column:Int = 5,
    list:List<AppEntrance> = emptyList(),
    onClick:(AppEntrance,Int)->Unit = {_,_->},
    onLongClick:(AppEntrance,Int)->Unit = {_,_->}
){
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally) {
        (0 until row).forEach { rowIndex ->
            Row(
                Modifier
                    .fillMaxWidth()
                    .weight(1f),
                verticalAlignment = Alignment.CenterVertically) {
                (0 until column).forEach { colIndex ->
                    val index = rowIndex * column + colIndex
                    AppItem(
                        modifier = Modifier.weight(1f).fillMaxSize(),
                        entrance =
                            if(index >= list.size) emptyAppEntrance
                            else list[index],
                        index = index,
                        onClick = onClick,
                        onLongClick = onLongClick
                    )
                }
            }
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun AppItem(
    modifier: Modifier = Modifier,
    entrance:AppEntrance = emptyAppEntrance,
    index:Int = 0,
    onClick:(AppEntrance,Int)->Unit = {_,_ ->},
    onLongClick:(AppEntrance,Int)->Unit = {_,_->}
){
    val modifierCol =
        if(entrance.isEmpty()) modifier
        else modifier
            .padding(2.dp)
            .background(Color(0xff000000), RoundRect.r(4))
            .padding(1.dp)
            .background(Color(0xffffffff), RoundRect.r(3))
            .combinedClickable(
                onClick = { onClick(entrance,index) },
                onLongClick = { onLongClick(entrance,index) }
            )
            .padding(1.dp)
    Box(modifier = modifierCol,
        contentAlignment = Alignment.Center) {
        if(entrance.isValid()){
            Column(
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = rememberDrawablePainter(drawable = entrance.icon()),
                    contentDescription = "Icon",
                    contentScale = ContentScale.Inside,
                    modifier = Modifier
                        .padding(4.dp)
//                        .aspectRatio(1f)
                )
                Text(
                    text = entrance.name(),
                    overflow = TextOverflow.Ellipsis,
                    maxLines = 1
                )
            }
        }
    }
}